package com.allenkerr.controller;

import com.allenkerr.model.Person;
import com.allenkerr.service.PersonService;
import com.allenkerr.vo.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 小柯(Allen Kerr) on 17.6.18 23:58.
 */
@RestController
@RequestMapping("/people")
public class PersonController {

    @Autowired
    private PersonService personService;

    /**
     * 分页查询Person
     */
    @GetMapping("/list")
//    public List<Person> findByPage(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
//                                   @RequestParam(value = "rows", required = false, defaultValue = "2") Integer rows) {
//        Person person = new Person();
//        person.setPage(page);
//        person.setRows(rows);
    public List<Person> findBypage(Person person) {
        List<Person> people = personService.findAll(person);
        return people;
    }

    /**
     * 根据Id查找Person
     */
    @GetMapping("/id/{id}")
    public Person findById(@PathVariable Integer id) {
        return personService.findById(id);
    }

    /**
     * 根据名字查找Person
     */
    @GetMapping("/name")
    public List<Person> findByName(String name) {
        return personService.findByName(name);
    }

    /**
     * 保存Person
     */
    @PostMapping("/save")
    public Person saveOne(Person person) {
        personService.saveOne(person);
        return person;//自增主键id的值已更新
    }

    /**
     * 查找年龄比给定值大的people
     */
    @GetMapping("/age/gt/{age}")
    public List<Person> findAgeGreaterThan(@PathVariable Integer age) {
        return personService.findAgeGreaterThan(age);
    }

    /**
     * 根据id删除person
     */
    @DeleteMapping("/id/{id}")
    public Result deleteById(@PathVariable Integer id) {
        return personService.deleteById(id);
    }

    /**
     * 根据name模糊查询people
     *
     * @param name
     * @return
     */
    @GetMapping("/like")
    public List<Person> selectByNameLike(String name) {
        return personService.selectByNameLike(name);
    }

    /**
     * 测试全局异常捕获
     */
    @GetMapping("/e")
    public void throwException() {
        int i = 1 / 0;
    }

}
